package org.hadoop.writable;
import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Objects;

public class LineCharCountWritable implements WritableComparable<LineCharCountWritable> {
    private String line;
    private Integer count;

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(line);
        out.writeInt(count);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        line = in.readUTF();
        count = in.readInt();
    }

    public String getLine() {
        return line;
    }

    public void setLine(String line) {
        this.line = line;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    @Override
    public int compareTo(LineCharCountWritable o) {
        return this.count - o.getCount();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        LineCharCountWritable that = (LineCharCountWritable) o;
        return Objects.equals(line, that.line) && Objects.equals(count, that.count);
    }

    @Override
    public int hashCode() {
        return Objects.hash(line, count);
    }
}
